﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace YGWebMobileApi.Data
{
    /// <summary>
    /// 移动端通信消息基类
    /// </summary>
    public abstract class BaseMobileMsg
    {
        /// <summary>
        /// 处理成功的返回码 8 个 0
        /// </summary>
        //public const string SUCCESS_CODE = "00000000";

       

        /// <summary>
        /// 通信消息格式（Json 或 Xml）
        /// </summary>
        public EMobileMsgFormat MsgFormat
        {
            get;
            protected set;
        }

        /// <summary>
        /// 移动端发送的通信消息 Head 部分，即 Xml 的 Head 部分。
        /// </summary>
        public MobileMsgHead Head
        {
            get;
            private set;
        }

        /// <summary>
        /// 通信的 Xml 数据
        /// </summary>
        protected MobileXml MobileXml
        {
            get;
            set;
        }

        /// <summary>
        ///  报文的 Body
        /// </summary>
        public MobileXml.FieldList Body
        {
            get
            {
                return this.MobileXml.Body;
            }
        }

        /// <summary>
        /// 移动端通信消息基类
        /// </summary>
        public BaseMobileMsg()
        {
            this.MsgFormat = EMobileMsgFormat.Unknown;
            this.Head = new MobileMsgHead();
            this.MobileXml = new MobileXml();
        }

        /// <summary>
        /// 将 json 字符串转化为 BaseMobileMsg 
        /// </summary>
        /// <param name="json">json 字符串</param>
        public virtual void FromJson(string json)
        {
            this.MsgFormat = EMobileMsgFormat.Json;
            this.MobileXml = MobileJsonConverter.ToXml(json);
            this.Head.FromXml(this.MobileXml.Head);
        }

        /// <summary>
        /// 将 BaseMobileMsg 转化为 json 字符串 
        /// </summary>
        /// <returns></returns>
        public virtual string ToJson()
        {
            if (this.MobileXml != null)
            {
                this.Head.ToXml(this.MobileXml.Head);
                return MobileJsonConverter.ToJosn(this.MobileXml);
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 将 xml 字符串转化为 BaseMobileMsg 
        /// </summary>
        /// <param name="xml">xml 字符串</param>
        public virtual void FromXml(string xml)
        {
            this.MsgFormat = EMobileMsgFormat.Xml;
            this.MobileXml.FromXml(xml);
            this.Head.FromXml(this.MobileXml.Head);
        }

        /// <summary>
        /// 将 BaseMobileMsg 转化为 xml 字符串 
        /// </summary>
        /// <returns></returns>
        public virtual string ToXml()
        {
            if (this.MobileXml != null)
            {
                this.Head.ToXml(this.MobileXml.Head);
                return this.MobileXml.ToXml();
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 将 BaseMobileMsg 以 UTF-8 编码转化为 byte 数组
        /// </summary>
        /// <returns></returns>
        public virtual byte[] ToBytes()
        {
            if (this.MsgFormat == EMobileMsgFormat.Json)
            {
                return Encoding.UTF8.GetBytes(this.ToJson());
            }
            else
            {
                return Encoding.UTF8.GetBytes(this.ToXml());
            }
        }
    }
}